package tc.de.krawallmann.gravoblocks.game;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import tc.de.krawallmann.achievement.Achievement;
import tc.de.krawallmann.achievement.AchievementsListener;
import tc.de.krawallmann.extension.AchievementExtension;
import tc.de.krawallmann.extension.SettingsExtension;
import tc.de.krawallmann.gravoblocks.resource.Language;
import tc.de.krawallmann.slick.game.AbstractExtendableGame;
import tc.de.krawallmann.slick.game.AbstractMMenuState;
import tc.de.krawallmann.twl.RootPane;
import tc.de.krawallmann.twl.widgets.MulticolorLabel;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.ScrollPane;
import de.matthiasmann.twl.ScrollPane.Fixed;
import de.matthiasmann.twl.Table;
import de.matthiasmann.twl.model.SimpleTableModel;

/**
 * The Class ScoresState displays all the achievements.
 *  @author KrawallMann
 * @version 1.0
 */
public class AchievementsState extends AbstractMMenuState implements AchievementsListener {
	/** The "scores" label. */
	private transient Label achievementsLabel;

	/** The backButton button. */
	private transient Button backButton;
	/** The click. */
	private transient Sound click;

	/** The scroll pane. */
	private transient ScrollPane scrollPane;

	/** The sum label. */
	private transient MulticolorLabel sumLabel;

	/** The table. */
	private transient Table table;

	/**
	 * Instantiates a new AchievementsState.
	 * 
	 * @param menuID
	 *            the menu's id
	 * @param game
	 *            the game
	 */
	public AchievementsState(final int menuID, final AbstractExtendableGame game) {
		super(menuID, game);
	}

	/* (non-Javadoc)
	 * @see tc.de.krawallmann.achievement.AchievementsListener#achievementUnlocked(tc.de.krawallmann.achievement.Achievement)
	 */
	@Override
	public void achievementUnlocked(final Achievement achievement) {
		reload();
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.slick.game.AbstractMMenuState#init(org.newdawn.
	 * slick.GameContainer, org.newdawn.slick.state.StateBasedGame)
	 */
	@Override
	public void init(final GameContainer container, final StateBasedGame game)
			throws SlickException {
		super.init(container, game);
		click = new Sound("tc/de/krawallmann/gravoblocks/resource/click.wav"); //$NON-NLS-1$
		setBackground(new Image("tc/de/krawallmann/gravoblocks/resource/mainmenu.jpg")); //$NON-NLS-1$
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.twlslick.BasicTWLGameState#layoutRootPane()
	 */
	@Override
	public void layoutRootPane() {
		super.layoutRootPane();

		// Adjust the sizes
		table.adjustSize();
		backButton.adjustSize();
		achievementsLabel.adjustSize();
		scrollPane.setSize(730, 350);
		sumLabel.adjustSize();

		// Set the positions
		table.setSize(700, Math.max(table.getHeight(), 350));
		scrollPane.setPosition(50, 120);
		backButton.setPosition(750 - backButton.getWidth(), 500);
		achievementsLabel.setPosition(
				(getContainer().getWidth() - achievementsLabel.getWidth()) / 2, 20);
		sumLabel.setPosition(50, scrollPane.getHeight() + 130);
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.twlslick.BasicTWLGameState#createRootPane()
	 */
	@Override
	protected RootPane createRootPane() {
		final RootPane pane = super.createRootPane();

		// Set root theme
		pane.setTheme(""); //$NON-NLS-1$

		// Create the widgets
		achievementsLabel = new Label(Language.getString("AchievementsState.0")); //$NON-NLS-1$
		achievementsLabel.setTheme("label60"); //$NON-NLS-1$

		table =
				new Table(
						new SimpleTableModel(
								new String[] {
										Language.getString("AchievementsState.1"), Language.getString("AchievementsState.8"), Language.getString("AchievementsState.2"), Language.getString("AchievementsState.3")})); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		table.setTheme("/table"); //$NON-NLS-1$
		int sum = 0;
		int ownsum = 0;
		for (final Achievement s : getGame().getPossibleAchievements()) {
			((SimpleTableModel) table.getModel())
					.addRow(s.getName(),
							s.isIngame() ? Language.getString("AchievementsState.4") : Language.getString("AchievementsState.5"), s.getPoints(), getGame().getExtension(AchievementExtension.class).isUnlocked(s) ? Language.getString("AchievementsState.4") : Language.getString("AchievementsState.5")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			sum += s.getPoints();
			if (getGame().getExtension(AchievementExtension.class).isUnlocked(s)) {
				ownsum += s.getPoints();
			}
		}
		sumLabel =
				new MulticolorLabel(
						ownsum
								+ "/" + sum + Language.getString("AchievementsState.7") + " - " + (int) Math.round((1 - (sum - ownsum) / (double) sum) * 100) + "%"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		sumLabel.setColor(MulticolorLabel.Color.RED);
		sumLabel.setTheme("label30"); //$NON-NLS-1$

		scrollPane = new ScrollPane(table);
		scrollPane.setFixed(Fixed.HORIZONTAL);

		backButton = new Button(Language.getString("ScoresState.4")); //$NON-NLS-1$
		backButton.setTheme("button40"); //$NON-NLS-1$

		// Add callbacks
		backButton.addCallback(new Runnable() {
			@Override
			public void run() {
				if (SettingsExtension.getSetting(
						GravoBlocksGame.USE_SOUND)) {
					click.play();
				}
				getGame().enterState(GravoBlocksGame.MAINMENUSTATE, new FadeOutTransition(),
						new FadeInTransition());
			}
		});

		// Add the widgets
		pane.add(achievementsLabel);
		pane.add(scrollPane);
		pane.add(backButton);
		pane.add(sumLabel);

		// Make the widgets focusable
		makeFocusable(backButton);

		return pane;
	}
}
